package com.colud.client.test;

import com.colud.client.JT809.*;
import com.colud.client.util.ClientUtils;
import com.colud.client.util.JT809Constants;
import com.colud.client.vo.Message;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;

import java.nio.charset.Charset;
import java.util.Calendar;
import java.util.Date;

import static com.colud.client.util.ClientUtils.*;

/**
 * @author mfy
 * @date 2021/10/24
 * @Desc 各种业务数据类型的数据体
 */
public class JT809BodyManager {

    /**
     * 获取T1003 数据体
     */
    public static Message T1003DataBody(T1003 awsVo) {
        Message msg = new Message(JT809Constants.UP_DICONNECE_REQ);
        ChannelBuffer buffer = ChannelBuffers.buffer(12);//数据体总长
        buffer.writeInt(awsVo.getUserId());//4
        buffer.writeBytes(getBytesWithLengthAfter(8, awsVo.getPassword().getBytes(Charset.forName("GBK"))));//8
        msg.setMsgBody(buffer);
        return msg;
    }

    /**
     * 获取T1201 数据体
     */
    public static Message T1201DataBody(T1201 awsVo) {
        Message msg = new Message(JT809Constants.UP_EXG_MSG);
        ChannelBuffer buffer = ChannelBuffers.buffer(21 + 1 + 2 + 4 + 11 + 11 + 20 + 7 + 12);//数据体总长
        buffer.writeBytes(getBytesWithLengthAfter(21, awsVo.getVehicleNo().getBytes(Charset.forName("GBK"))));//21
        buffer.writeByte(1);//1
        buffer.writeShort(JT809Constants.UP_EXG_MSG_REGISTER);//2
        buffer.writeInt(11 + 11 + 20 + 7 + 12);//4
        buffer.writeBytes(getBytesWithLengthAfter(11, "平台53079".getBytes(Charset.forName("GBK"))));//11
        buffer.writeBytes(getBytesWithLengthAfter(11, awsVo.getProducerId().getBytes()));//11
        buffer.writeBytes(getBytesWithLengthAfter(20, awsVo.getTerminalModelType().getBytes()));//8
        buffer.writeBytes(getBytesWithLengthAfter(7, awsVo.getTerminalId().getBytes()));//7
        buffer.writeBytes(getBytesWithLengthAfter(12, awsVo.getTerminalSimcode().getBytes()));//12
        msg.setMsgBody(buffer);
        return msg;
    }

    /**
     * 获取T1202 数据体
     */
    public static Message T1202DataBody(T1202 awsVo) {
        Message msg = new Message(JT809Constants.UP_EXG_MSG);
        ChannelBuffer buffer = ChannelBuffers.buffer(64);//数据体总长
        buffer.writeBytes(getBytesWithLengthAfter(21, awsVo.getVehicleNo().getBytes(Charset.forName("GBK"))));//21
        buffer.writeByte(1);//1
        buffer.writeShort(JT809Constants.UP_EXG_MSG_REAL_LOCATION);//2
        buffer.writeInt(36);//4
        //是否加密
        buffer.writeByte((byte) 0);//0未加密 // 1
        //日月年dmyy
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        buffer.writeByte((byte) cal.get(Calendar.DATE));
        buffer.writeByte((byte) (cal.get(Calendar.MONTH) + 1));
        String hexYear = "0" + Integer.toHexString(cal.get(Calendar.YEAR));
        buffer.writeBytes(hexStringToByte(hexYear));//4
        //时分秒
        buffer.writeByte((byte) cal.get(Calendar.HOUR_OF_DAY));
        buffer.writeByte((byte) cal.get(Calendar.MINUTE));
        buffer.writeByte((byte) cal.get(Calendar.SECOND));//3
        //经度，纬度
        buffer.writeInt(formatLonLat(Double.parseDouble(awsVo.getLon()) / 1000000));//4
        buffer.writeInt(formatLonLat(Double.parseDouble(awsVo.getLat()) / 1000000));//4
        //速度 km/h 数据库中百米/时
        if (awsVo.getVec1() == 0) {
            buffer.writeShort(awsVo.getVec1());//2
        } else {
            buffer.writeShort(awsVo.getVec1() / 10);//2
        }
        //行驶记录速度
        buffer.writeShort(awsVo.getVec2());//2
        //车辆当前总里程数
        buffer.writeInt((int) awsVo.getVec3());//4
        //方向
        buffer.writeShort(awsVo.getDirection());//2
        //海拔 m
        //buffer.writeShort((short) 0);//2
        buffer.writeShort(awsVo.getAltitude());//2
        //车辆状态
        //int accStatus = 0;
        //int gpsStatus = 0;
        //if (accStatus == 0 && gpsStatus == 0) {
        //    buffer.writeInt(0);//4
        //} else if (accStatus == 1 && gpsStatus == 0) {
        //    buffer.writeInt(1);//4
        //} else if (accStatus == 0 && gpsStatus == 1) {
        //    buffer.writeInt(2);//4
        //} else {
        //    buffer.writeInt(3);//4
        //}
        buffer.writeInt((int) awsVo.getState());
        //报警状态
        buffer.writeInt((int) awsVo.getAlarm());//0表示正常；1表示报警//4
        msg.setMsgBody(buffer);
        return msg;
    }

    /**
     * 获取T1207 数据体
     */
    public static Message T1207DataBody(T1207 awsVo) {
        Message msg = new Message(JT809Constants.UP_EXG_MSG);
        ChannelBuffer buffer = ChannelBuffers.buffer(21 + 1 + 2 + 4 + 8 + 8);//数据体总长
        buffer.writeBytes(getBytesWithLengthAfter(21, awsVo.getVehicleNo().getBytes(Charset.forName("GBK"))));//21
        buffer.writeByte(awsVo.getVehicleColor());//1
        buffer.writeShort(awsVo.getDataType());//2
        buffer.writeInt(8 + 8);//4
        Date startTime = getUTCTimeStr(awsVo.getStartTime());
        System.out.println(startTime);
        Date endTime = getUTCTimeStr(awsVo.getEndTime());
        //buffer.writeBytes(getBytesWithLengthAfter(8, startTime.getBytes(Charset.forName("GBK"))));
        //buffer.writeBytes(getBytesWithLengthAfter(8, endTime.getBytes(Charset.forName("GBK"))));
        buffer.writeBytes(ClientUtils.longToBytes(startTime.getTime()));//8
        buffer.writeBytes(ClientUtils.longToBytes(endTime.getTime()));//8
        msg.setMsgBody(buffer);
        return msg;
    }

    /**
     * 获取T1208 数据体
     */
    public static Message T1208DataBody(T1208 awsVo) {
        Message msg = new Message(JT809Constants.UP_EXG_MSG);
        ChannelBuffer buffer = ChannelBuffers.buffer(21 + 1 + 2 + 4);//数据体总长
        buffer.writeBytes(getBytesWithLengthAfter(21, awsVo.getVehicleNo().getBytes(Charset.forName("GBK"))));//21
        buffer.writeByte(awsVo.getVehicleColor());//1
        buffer.writeShort(awsVo.getDataType());//2
        buffer.writeInt(0);//4
        msg.setMsgBody(buffer);
        return msg;
    }
    /**
     * 获取T1208 数据体
     */
    public static Message T920BDataBody(T1208 awsVo) {
        Message msg = new Message(JT809Constants.DOWN_EXG_MSG);
        ChannelBuffer buffer = ChannelBuffers.buffer(21 + 1 + 2 + 4);//数据体总长
        buffer.writeBytes(getBytesWithLengthAfter(21, awsVo.getVehicleNo().getBytes(Charset.forName("GBK"))));//21
        buffer.writeByte(awsVo.getVehicleColor());//1
        buffer.writeShort(awsVo.getDataType());//2
        buffer.writeInt(0);//4
        msg.setMsgBody(buffer);
        return msg;
    }


    /**
     * 获取T120B 数据体
     */
    public static Message T120BDataBody(T120B awsVo) {
        Message msg = new Message(JT809Constants.UP_EXG_MSG);
        ChannelBuffer buffer = ChannelBuffers.buffer(21 + 1 + 2 + 4 + 4 + 20);//数据体总长
        buffer.writeBytes(getBytesWithLengthAfter(21, awsVo.getVehicleNo().getBytes(Charset.forName("GBK"))));//21
        buffer.writeByte(awsVo.getVehicleColor());//1
        buffer.writeShort(awsVo.getDataType());//2
        buffer.writeInt(4 + 20);//运单长度暂定 20
        buffer.writeInt(awsVo.getEwaybillLength());
        buffer.writeBytes(getBytesWithLengthAfter(20, awsVo.getEwaybillInfo().getBytes(Charset.forName("GBK"))));
        msg.setMsgBody(buffer);
        return msg;
    }

    /**
     * 获取T1402 数据体
     */
    public static Message T1402DataBody(T1402 awsVo) {
        Message msg = new Message(JT809Constants.UP_WARN_MSG);
        ChannelBuffer buffer = ChannelBuffers.buffer(21 + 1 + 2 + 4 + 1 + 2 + 8 + 4 + 4 + 20);//数据体总长
        buffer.writeBytes(getBytesWithLengthAfter(21, awsVo.getVehicleNo().getBytes(Charset.forName("GBK"))));//21
        buffer.writeByte(awsVo.getVehicleColor());//1
        buffer.writeShort(awsVo.getDataType());//2
        buffer.writeInt(1 + 2 + 8 + 4 + 4 + 20);//报警内容长度暂定 20
        buffer.writeByte(awsVo.getWarnSrc());//1
        buffer.writeShort(awsVo.getWarnType());//2
        Date startTime = getUTCTimeStr(awsVo.getWarnTime());
        buffer.writeBytes(ClientUtils.longToBytes(startTime.getTime()));//8
        buffer.writeInt(awsVo.getInfoId());
        buffer.writeInt(awsVo.getInfoLength());
        buffer.writeBytes(getBytesWithLengthAfter(20, awsVo.getInfoContent().getBytes(Charset.forName("GBK"))));
        msg.setMsgBody(buffer);
        return msg;
    }
}
